﻿using FastBuild.TimeJob;
using Wechat_PublicNumber.Common;
using Wechat_PublicNumber.Model;
using Wechat_PublicNumber.Repository;

namespace Wechat_PublicNumber.Jobs
{
    [SkipWhileExecuting]
    public class WechatPushTempJob : DataAccess, IJob
    {

        #region DI
        [Autowired]
        private WxHttpInvoke _wxHttpInvoke;

        [Autowired]
        private WxTemplateRepository _templateRepository;

        #endregion

        public async Task Execute()
        {
            try
            {
                _logger.Info("WechatPushTempJob Begin", "WechatPushTempJob");

                var openIdList = await _wxHttpInvoke.UserOpenIdList();
                var pushData = await _templateRepository.GetTemplateData(WeChatTemplateEnum.Weather, openIdList.data.openid);
                if (pushData is not null) await _wxHttpInvoke.PushTemplate(pushData);

                _logger.Info($"WechatPushTempJob End，Push Count:{openIdList.data.openid.Count}条", "WechatPushTempJob");

                GC.Collect();
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "WechatPushTempJob Error", "WechatPushTempJob");
            }
        }
    }
}
